/**
 * Copyright (c) 2013-4-12 上午11:13:29 WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.extblog.wkm.web.vo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * @author WKM
 * Service.java history
 * 1.WKM - 2013-4-12 - Create File
 */
public class UserDetailsView implements UserDetails {

	/**
	 * 序列唯一
	 */
	private static final long serialVersionUID = 1L;
	
	/** 日志信息 **/
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	/** 角色类型 **/
	private List<String> roles = null;
	
	/** 用户MD5密码 **/
	private String password = null;
	
	/** 用户名称 **/
	private String username = null;

	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#getAuthorities()
	 */
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		// TODO Auto-generated method stub
		List<GrantedAuthority> roleNames = new ArrayList<GrantedAuthority>();
		if(roles == null){
			logger.warn("空的角色名称:" + roles);
			return roleNames;
		}
		Iterator<String> it = roles.iterator();
		while(it.hasNext()){
			roleNames.add(new SimpleGrantedAuthority(it.next()));
		}
		logger.info("用户角色名称包括:" + roleNames);
		return roleNames;
	}
	
	/**
	 * 设置该用户所属角色
	 * @param roles
	 */
	public void setAuthorities(List<String> roles){
		this.roles = roles;
	}

	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#getPassword()
	 */
	@Override
	public String getPassword() {
		// TODO Auto-generated method stub
		if(StringUtils.isBlank(password)){
			logger.warn("密码为空:" + password);
			return StringUtils.EMPTY;
		}
		return password;
	}

	/**
	 * 设置密码 MD5
	 * @param password
	 */
	public void setPassword(String password) {
		// TODO Auto-generated method stub
		this.password = password;
	}
	
	
	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#getUsername()
	 */
	@Override
	public String getUsername() {
		// TODO Auto-generated method stub
		if(StringUtils.isBlank(username)){
			logger.warn("用户名为空:" + username);
			return StringUtils.EMPTY;
		}
		return username;
	}
	
	/**
	 * 设置用户名称
	 * @param username
	 */
	public void setUsername(String username) {
		// TODO Auto-generated method stub
		this.username = username;
	}

	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonExpired()
	 */
	@Override
	public boolean isAccountNonExpired() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonLocked()
	 */
	@Override
	public boolean isAccountNonLocked() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#isCredentialsNonExpired()
	 */
	@Override
	public boolean isCredentialsNonExpired() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#isEnabled()
	 */
	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return false;
	}
}
